
function navigationCtrl($scope,$rootScope,$modal){

    $scope.loadUser = function(){
        loadUser($rootScope);
    }
    $scope.readDbInstance = function(){
        readDbInstance($scope);
    }

   /* $scope.readSchemaList = function($scope,dbId){
        readSchemaByDbId($scope,dbId);
    }*/

   /* //数据库实例切换时，改变对应的table
    $scope.dbChange = function(){
        readSchemaByDbId($scope);
    }*/
    $scope.changeSchema = function(db,schema){
        //点亮图标
        $(".schema-li").removeClass("active");
        $(".schema-li[schemaid='"+schema.id+"']").addClass("active");

        var arr = new Array();
        for(var i=0;i<$rootScope.sourceSqlLogList.length;i++){
            var sqlLog = $rootScope.sourceSqlLogList[i];
            if(sqlLog.schemaName==schema.schemaName){
                arr.push(sqlLog);
            }
        }
        $rootScope.showSqlLogList = arr;

        $rootScope.currSchemaName = schema.schemaName;
        $rootScope.currDbId = db.dbId;
        $rootScope.currDbName = db.dbRemark==''?db.dbName:db.dbRemark;
    }

    $scope.openBox_update_remark = function(){
        readUserSchemaList_nav($scope,$rootScope);

        var _c = setInterval(function() {
            if ($scope.zNodes == null) {
                return;
            }
            var $modalInstance = $modal.open({
                templateUrl: 'views/sql_remark_update_page.html',
                controller: Nvi_ModalCtrl,
                scope: $scope
            });
            clearInterval(_c);
        },100);
    }

    $scope.setting = {
        data : {
            simpleData : {
                enable : true,
                idKey : "id",
                pIdKey : "pId",
                rootPId : 0
            }
        },
        callback : {
            beforeClick : zTreeBeforeClick
        }
    };
}

var currNode;
function zTreeBeforeClick(treeId, treeNode, clickFlag){
    currNode = treeNode;
}

//加载用户的信息,包括SchemaList和DbRemark
function readUserSchemaList_nav($scope){
    var arr = new Array();
    var root = {'id':1,'pid':0,'name':'root',open:true};
    arr.push(root);
    var count = 2;
    $.ajax({
        url: '/user/doReadDbRemarkAndSchemaByUserId',
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.$apply(function () {
                    $scope.dbRemarkList = responseObj.dbRemarkList;

                    $.each($scope.dbRemarkList, function(i, v) {
                        var pId = count++;
                        var showName = v.dbName;
                        if(v.dbRemark){
                            showName += '-('+v.dbRemark+')';
                        }
                        arr.push({id:pId,name: showName,t:v.url,pId:1,open:true,t: v.dbId});
                        $.each(v.schemaRemarkList,function(j,e){
                            var showName = e.schemaName;
                            if(e.schemaName!=e.schemaRemark){
                                showName+='-('+ e.schemaRemark+')';
                            }
                            arr.push({id:count++,name: showName ,pId:pId, t: e.schemaId});
                        });
                    });

                    $scope.zNodes = arr;
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function Nvi_ModalCtrl($scope, $modalInstance) {

    //更新操作
    $scope.update = function () {
        if(confirm('确认更新操作吗?')){
            $modalInstance.close();
        }
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

    $scope.remarkCurrNode = function(){
        if(!currNode){
            alert('请点击任意节点进行备注');
            return;
        }
        if(currNode.pId==0){
            alert('无法对root进行操作');
            return;
        }
        var _remark = '';
        var name = currNode.name;
        var idx = name.indexOf('-(');
        var orgName = '';
        if(idx!=-1){
            _remark = name.substring(idx+2,name.length-1);
            orgName = name.substring(0,idx);
        }
        var remark = window.prompt('请输入备注名称',_remark);
        remark = remark==''?orgName:remark;
        if($.trim(remark)==''){
            return;
        }
        for(var i=0;i<$scope.zNodes.length;i++){
            if($scope.zNodes[i].id==currNode.id){
                var name = $scope.zNodes[i].name;
                if(name.indexOf('-(')!=-1){
                    $scope.zNodes[i].name = name.substring(0,name.indexOf('-('))+'-('+remark+')';
                }else{
                    $scope.zNodes[i].name+='-('+remark+')';
                }
            }
        }
        if(currNode.pId==1){//上级节点是root,本级节点就是数据库了
            updateDbRemark($scope,currNode.t,remark);
        }else{//Schema节点
            updateSchemaRemark($scope,currNode.t,remark);
        }
        $modalInstance.close();
    }

};

//更新Schema的备注名称
function updateSchemaRemark($scope,id,remark){
    var paramObj = new Object();
    paramObj.schemaId = id;
    paramObj.schemaRemark = remark;
    $.ajax({
        url: '/db/doUpdateSchemaRemarkBySchemaId',
        data: paramObj,
        type:'post',
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                for(var i=0;i<$scope.dbinstance.length;i++){
                    var db = $scope.dbinstance[i];
                    for(var j=0;j<db.schemaRemarkList.length;j++){
                        if(db.schemaRemarkList[j].schemaId==id){
                            db.schemaRemarkList[j].schemaRemark = remark;
                        }
                    }
                }
                alert('更新成功');
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

//更新DB的remark
function updateDbRemark($scope,id,remark){
    var paramObj = new Object();
    paramObj.dbId = id;
    paramObj.dbRemark = remark;
    $.ajax({
        url: '/db/doUpdateDbRemarkByDbId',
        data: paramObj,
        type:'post',
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                alert('更新成功');
                for(var i=0;i<$scope.dbinstance.length;i++){
                    if($scope.dbinstance[i].dbId==id){
                        $scope.dbinstance[i].dbRemark = remark;
                    }
                }

            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function loadUser($rootScope){
    if($rootScope.systemUser){
        return;
    }
    $.ajax({
        url: '/user/doReadSystemUser',
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $rootScope.$apply(function () {
                    $rootScope.systemUser = responseObj.systemUser;
                });
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}
function readDbInstance($scope){
    $.ajax({
        url: '/user/doReadDbInstances',
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.$apply(function () {
                    $scope.dbinstance = responseObj.dataList;
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}
/*function readSchemaByDbId($scope){
    var paramObj = new Object();
    paramObj.dbId = $scope.select1;
    $.ajax({
        url: '/user/doReadSchemaByUserIdAndDbId',
        data:paramObj,
        success: function (data) {
            var responseObj = $.parseJSON(data);
            if (responseObj.success) {
                $scope.$apply(function () {
                    $scope.schemas = responseObj.dataList;
                });
            } else {
                alert(responseObj.errorMsg);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });

}*/

angular
    .module('inspinia')
    .controller('navigationCtrl', navigationCtrl)